% ML TAU 2013 final project script
% Alt 4:
% Calculate abs(S1 - S2) and perform SVM based on its L to H PCA

%base_path = '/home/itay/TAU/IML/final';
%libsvm_path = '/opt/libsvm-3.17/matlab';

% Add path to the libsvm
%addpath(base_path);
%addpath(libsvm_path);

function gogo_alt4()


    %
    % Initialization
    %
    close all; clear; clc; tic;
    
    N = 21; %number of results to be saved.
    
    if ~exist('dataforproject.mat','file')
        error('Data file not found in current directory')
    end;
    
    % Saving memory. Loading vars on a need to load basis only throughout
    load('dataforproject.mat','X1train','X2train','gidtrain','ytrain');
    fprintf('Training Data successfully loaded\n');

    X_DELTA = abs(X1train-X2train);

    [m,n] = size(X_DELTA);
    
    X_NORM = X_DELTA/max(X_DELTA(:));

    [X_PC COEFF SCORE latent ] = basic_PCA(X_NORM', n, 'X1train-X2train');
    

L_vec = 3:3;% 2:5;

H_vec = 120:120; %[50,80,100,120,150,200,500,1000];

ker_degrees = 2:6;%1:4;

ker_types = [3,1];%0:3;

c_degrees = -4:4;%-3:7;

best_results = zeros(N,6) ; i = 1;

[L H C D T Err] = svm_grid_search(ker_types,ker_degrees,c_degrees,L_vec,H_vec);
fprintf('===\tResults\t===\n(L*=%d ; H*=%d ; C*=%f ; t*=%d ; deg*=%d)\n#Avg Acc: %f\n',L,H,C,T,D,1-Err);
save(sprintf('best_%d_results.mat',N), 'best_results');
disp(best_results);

function [L H C D T Err] = svm_grid_search(ker_types,ker_degrees,c_degrees,L_vec,H_vec)
    
    s = size (c_degrees);
    
    c_vec = power(repmat(2,s),c_degrees);   
 
    Err = 1.0; L = L_vec(1); H = H_vec(1);

for  t=ker_types
   
    for d=ker_degrees
        
        for c=c_vec
            
            for l=L_vec
                
                for h=H_vec
                    
                    e = 1-do_svm(l,h,t,c,d); 
                    if e < Err
                        C=c; D=d; T=t; Err=e; H=h; L=l;
                    end
                    if e < 0.3
                        best_results(mod(i,N)+1,:) =[l h c d t e];
                        i = i+1;
                    end
                end
            end         
        end    
    end
end

end


function [accuracy] = do_svm(L,H,t,c,d)

    TX = X_PC(:,L:H);
    
    accuracy = 0.0;

    for k=1:3

        % Training set
        X = TX((gidtrain~=k),:);

        y = ytrain(gidtrain~=k);

        % Create the model
        model = svmtrain(y,X,sprintf('-t %d -g  1.0 -c %f -d %d',t,c,d));

        % Testing set
        X = TX((gidtrain==k),:);
        y = ytrain(gidtrain==k);

        % Predict
        predictedY = svmpredict(y,X,model);

        results = sum(y.*predictedY > 0) / size(y,1); 
        
        accuracy = accuracy+results;

        %fprintf('Results: %f\n', results);
    end
    
    accuracy = accuracy/3;
end

end

